package com.health.userbackstage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.userbackstage.exception.BusinessException;
import com.health.userbackstage.mapper.OrderSettingMapper;
import com.health.userbackstage.mapper.UserMapper;
import com.health.userbackstage.pojo.OrderSetting;
import com.health.userbackstage.pojo.User;
import com.health.userbackstage.pojo.result.ResultEnum;
import com.health.userbackstage.service.OrderSettingService;
import com.health.userbackstage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author ASUS
 * @Date 2023/8/18 21:19
 * @Version 1.0
 */
@Service
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {

    @Autowired
    private OrderSettingMapper osm;

    @Autowired
    private SimpleDateFormat sdf;

    @Override
    public List<OrderSetting> findByDateRange(String min, String max) {
        return osm.findByDateRange(min,max);
    }

    @Override
    public List<OrderSetting> findByDate(String date) {
        return osm.findByDate(date);
    }

    @Override
    public boolean updateNumber(OrderSetting os) {
        Date orderDate = os.getOrderDate();
        String date = sdf.format(orderDate);
        OrderSetting editDay = osm.findOne(date);
//        如果该天没有被设置过预约 则新增
        if(editDay==null){
            osm.insert(os);
            return true;
//            如果该天已经有预约设置 则修改
        }else if(os.getNumber()>=editDay.getReservations()) {
            os.setId(editDay.getId());
            osm.updateById(os);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List getOrderDates() {
        List<Date> orderDates = osm.getOrderDates();
//        Collections.sort(orderDates);
        List list = new ArrayList();
        for (Date orderDate:orderDates) {
            String str = (orderDate.getYear()+1900)+"-"+(orderDate.getMonth()+1)+"-"+orderDate.getDate();
            if(!list.contains(str)){
                list.add(str);
            }
        }
        Collections.sort(list);
        return list;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateReservationsByOrderDate(Date orderDate) {
        int i = osm.updateReservationsByOrderDate(orderDate);
        if(i > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteOrderReportByOrderId(Integer orderId) {
        int a = osm.deleteOrderReportByOrderId(orderId);
        if(a > 0){
            return true;
        }
        return false;
    }

    @Override
    public Date selectIdByDate(Date date) {
        Date count = osm.selectIdByDate(date);
        return count;
    }


}
